package com.sinba.common.business.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.sinba.common.business.dao.ISharePriceTrendDAO;
import com.sinba.common.business.entity.Sell;
import com.sinba.common.business.entity.SharePriceTrendEntity;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.user.service.IRegisterUserService;
import com.sinba.common.utils.DoubleFormat;

public class SharePriceTrendService implements ISharePriceTrendService {

	@Autowired
	private ISharePriceTrendDAO sharePriceTrendDAO;
	
	@Autowired
	private ISellService sellService;
	
	@Autowired
	private IRegisterUserService registerUserService;
	
	@Transactional
	public void save(SharePriceTrendEntity entity) {
		// TODO Auto-generated method stub
		sharePriceTrendDAO.save(entity);
	}

	@Transactional
	public List<SharePriceTrendEntity> findSharePriceTrend(String type) {
		// TODO Auto-generated method stub
		SharePriceTrendEntity query = new SharePriceTrendEntity();
		query.setTypes(type);
		PageDTO pageDto = sharePriceTrendDAO.findPager(query, 0, 20);
		
		List<SharePriceTrendEntity> list = new ArrayList<SharePriceTrendEntity>(20);
		list = (List<SharePriceTrendEntity>)pageDto.getData();
		
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int MINUTE = calendar.get(Calendar.MINUTE);
		
		if(MINUTE>=0 && MINUTE<20){
			//00
			calendar.set(Calendar.MINUTE, 0);
		}
		if(MINUTE>=20 && MINUTE<40){
			//20
			calendar.set(Calendar.MINUTE, 20);
		}

		if(MINUTE>=40 && MINUTE<59){
			//40
			calendar.set(Calendar.MINUTE, 40);
		}
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		int max = 0;
		if(list!=null){
			max = list.size();
		}
		for(int i=0;i<max;i++){
			calendar.set(Calendar.MINUTE,  calendar.get(Calendar.MINUTE)-20);
			list.get(i).setShowTime(sdf.format(calendar.getTime()));
		}
		
		Collections.reverse(list);
		
		return list;
	}
	
	@Transactional
	public void autoGet(){
		
		//删除超时会员
		registerUserService.deleteTimeOutUser();
		
		SharePriceTrendEntity entity = new SharePriceTrendEntity();
		
		Date date = new Date();
		entity.setGatheringTime(date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int MINUTE = calendar.get(Calendar.MINUTE);
		
		if(MINUTE>=0 && MINUTE<20){
			//00
			calendar.set(Calendar.MINUTE, 0);
		}
		if(MINUTE>=20 && MINUTE<40){
			//20
			calendar.set(Calendar.MINUTE, 20);
		}

		if(MINUTE>=40 && MINUTE<59){
			//40
			calendar.set(Calendar.MINUTE, 40);
		}
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		entity.setShowTime(sdf.format(calendar.getTime()));
		
		//0.001-0.009随机涨
		java.util.Random random=new java.util.Random();// 定义随机类
		int result=random.nextInt(10);
		Map<Integer, Double> maps = new HashMap<Integer, Double>();
		maps.put(1, 0.001);
		maps.put(2, 0.002);
		maps.put(3, 0.003);
		maps.put(4, 0.004);
		maps.put(5, 0.005);
		maps.put(6, 0.006);
		maps.put(7, 0.007);
		maps.put(8, 0.008);
		maps.put(9, 0.009);
		maps.put(10, 0.001);
		Sell sell = sellService.findTop1SellOrder();
		Double currentPrice = 0D;
		Double guidePrice = 0D;
		if(sell!=null && sell.getSellPrice()!=null){
			if(sell.getSellPrice()>0.2){
				currentPrice =0.2D;
			}else{
				currentPrice = sell.getSellPrice();
			}
			if(currentPrice==0){
				guidePrice=0D;
			}else{
				guidePrice = DoubleFormat.format(currentPrice+maps.get(result));
			}
		}
		if(guidePrice>0.2){
			guidePrice =0.2D;
		}
		entity.setCurrentPrice(currentPrice);
		entity.setGuidePrice(guidePrice);
		this.save(entity);
		
		
	
	}
	
	
	
	
}
